/**
 * 
 */

$(document).ready(function() {
	
	//展开与收缩
	$("body").on("click",".img",function(){
		var id = $(this).parent().find("span").html();
		var imgPath = $(this).attr("src");
		var m_this = $(this);
		$.get(
			"admin/type",
			{"id":id},
			function(result){
				if(imgPath == "images/-.gif"){
					m_this.attr("src","images/+.gif");
					for (var i = 0; i < result.length; i++) {
						$(".tr3").remove("#"+result[i].id);
					}
				}else{
					m_this.attr("src","images/-.gif");
					for (var i = 0; i < result.length; i++) {
						m_this.parent().parent().after("<tr class='tr3 pid"+result[i].pid+"' id='" + result[i].id +"' >"
								+"	<td class='td1'><img alt='' src=''>&nbsp<span>" + result[i].id +"</span></td>"
								+"	<td class='td2'><input type='text' value='" + result[i].name + "'> </td>"
								+"	<td class='td3'><span>"+result[i].pid+"</span></td>"
								+"	<td class='td4'><button class='editType'>修改</button><button class='deleteType'>删除</button> </td>"
								+"</tr>"
						);
					}
				}
			}
		)
	});
	//修改类别
	$("body").on("click",".editType",function(){
		var type = new Object();
		type.id = $(this).parent().parent().find(".td1").find("span").html();
		type.name = $(this).parent().parent().find(".td2").find("input").val();
		type.pid = $(this).parent().parent().find(".td3").find("span").html();
		  
		var options = {  
		    url: 'admin/editType',  
		    method: 'post', // 注意这里，传递对象给后台，这里必须是 POST 否则无法将对象封装到POST的body中流  
		    dataType: 'json',  
		    contentType: 'application/json', // 注意这里，传递对象给后台，这里必须是 application/json  
		    data: JSON.stringify(type), // 注意这里，传递对象给后台，这里必须将对象进行序列化  
		    success: function (rs) {  
		    	if(rs)
		        	alert("修改成功");
		        else
		        	alert("修改失败");
		    },  
		    error: function (rs) {  
	        	alert("修改失败");
		    }  
		};  
		$.ajax(options);  
	});
	//删除类别
	$("body").on("click",".deleteType",function(){
		var id = $(this).parent().parent().find(".td1").find("span").html();
		var pid = $(this).parent().parent().find(".td3").find("span").html();
		$.get(
			"admin/deleteType",
			{"id":id,"pid":pid},
			function(result){
				if(result){
					if(pid == 0){
						$(".tr2").remove("#"+id);
						$(".tr3").remove(".pid"+id);
						$("option").remove("."+id);
					}else{
						$(".tr3").remove("#"+id);
					}
					alert("删除成功");
				} else
					alert("删除失败");
			}
		)
	});
	//初始化页面加载
	$.get(
		"admin/initType",
		function(result){
			$("#typeSel").append("<option value='0'> ---无--- </option>");
			for (var i = 0; i < result.length; i++) {
				//alert("id = " + result[i].id +";name = " + result[i].name + ";pid = " + result[i].pid);
				if(result[i].pid == 0){
					$(".myTable").append("<tr class='tr2' id='" + result[i].id +"' >"
						+"	<td class='td1'><img class='img' alt='' src='images/+.gif'>&nbsp<span>" + result[i].id +"</span></td>"
						+"	<td class='td2'><input type='text' value='" + result[i].name + "'> </td>"
						+"	<td class='td3'><span>"+result[i].pid+"</span></td>"
						+"	<td class='td4'><button class='editType'>修改</button><button class='deleteType'>删除</button> </td>"
						+"</tr>"
					);
					$("#typeSel").append("<option class="+result[i].id+" value="+result[i].id+">"+ result[i].name +"</option>");
					
				}
/*				else {
					$("#"+result[i].id).after("<tr class='tr3' id='" + result[i].id +"' class='"+result[i].pid+"'>" 
						+"	<td class='td1'><img alt='' src=''>&nbsp<span>" + result[i].id +"</span></td>"
						+"	<td class='td2'><input type='text' value='" + result[i].name + "'> </td>"
						+"	<td class='td3'><span>"+result[i].pid+"</span></td>"
						+"	<td class='td4'><button class='editType'>修改</button><button class='deleteType'>删除</button> </td>"
						+"</tr>"
					);
				}*/
			}
		}
	);
	
	$("#submit").click(function(){
		var name = $("#name").val();
		var pid = $("#typeSel option:selected").val();
		$.ajax({
			type:"get",
			url :"admin/addType?name="+name+"&pid="+pid,
			dateType:"json",
			success:function(result,error){
				if(result){
					if(pid == 0){
						$(".myTable").append("<tr class='tr2' id='" + result +"' >"
							+"	<td class='td1'><img class='img' alt='' src='images/+.gif'>&nbsp<span>" + result +"</span></td>"
							+"	<td class='td2'><input type='text' value='" + name + "'> </td>"
							+"	<td class='td3'><span>"+pid+"</span></td>"
							+"	<td class='td4'><button class='editType'>修改</button><button class='deleteType'>删除</button> </td>"
							+"</tr>"
						);
						$("#typeSel").append("<option class="+result+" value="+result+">"+ name +"</option>");
					}else{
						var m_this = $("#"+pid).find("img");
						if(m_this.attr("src") == "images/-.gif"){
							m_this.parent().parent().after("<tr class='tr3 pid"+pid+"' id='" + result +"' >"
									+"	<td class='td1'><img alt='' src=''>&nbsp<span>" + result +"</span></td>"
									+"	<td class='td2'><input type='text' value='" + name + "'> </td>"
									+"	<td class='td3'><span>"+pid+"</span></td>"
									+"	<td class='td4'><button class='editType'>修改</button><button class='deleteType'>删除</button> </td>"
									+"</tr>"
							);
						}
					}
					alert("添加成功");
				}else{
					alert("添加失败");
				}
			},
			error: function(){
				alert("添加失败");
			}
			
		});
	});
})